package com.eric.windows.actionbar;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;

import com.eric.windows.R;

import java.util.ArrayList;

public class ActionBarListActivity extends Activity {

	private FragmentTransaction fragmentTransaction;
	/**当前显示的Fragment 的索引 */
	private int currentFragmentIndex = 9999;
	/**Fragment集合*/
	private ArrayList<Fragment> fragments = new ArrayList<>();
	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_action_tabs);

		fragments.add(new ActionFragment());
		fragments.add( new ActionFragment2());
		fragments.add( new ActionFragment3());

		//获取该Activity的ActionBar：
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,android.R.id.text1,new String[]{"第一页","第二页","第三页"});

		fragmentManager = getFragmentManager();
/*		fragmentTransaction = fragmentManager.beginTransaction();
//		默认显示第一页
		fragmentTransaction.add(R.id.frame_action_container,fragments.get(0));
		fragmentTransaction.commit();*/

		actionBar.setListNavigationCallbacks(arrayAdapter, new ActionBar.OnNavigationListener() {
			@Override
			public boolean onNavigationItemSelected(int itemPosition, long itemId) {
				if (currentFragmentIndex >=0 && currentFragmentIndex < fragments.size()){
					fragments.get(currentFragmentIndex).onStop();
				}
				switch (itemPosition){
					case 0:
						if (currentFragmentIndex != 0){
							fragmentTransaction = fragmentManager.beginTransaction();
							if (!fragments.get(0).isAdded()){
								//如果要显示的Fragment还没有添加过，则需要添加到fragmentTransaction中
								fragmentTransaction.add(R.id.frame_action_container,fragments.get(0));
							}else {//如果要显示的Fragment已经添加过，则隐藏当前的fragment，展现要显示的Fragment
								fragments.get(0).onStart();
								if (currentFragmentIndex >=0 && currentFragmentIndex < fragments.size()){
									fragmentTransaction.hide(fragments.get(currentFragmentIndex));
								}
								fragmentTransaction.show(fragments.get(0));
							}
							//提交业务
							fragmentTransaction.commit();
							//保存当前的Fragment的索引
							currentFragmentIndex = 0;
						}
						break;
					case 1:
						if (currentFragmentIndex != 1){
							fragmentTransaction = fragmentManager.beginTransaction();
							if (!fragments.get(1).isAdded()){
								fragmentTransaction.add(R.id.frame_action_container,fragments.get(1));
							}else {
								fragments.get(1).onStart();
								if (currentFragmentIndex >=0 && currentFragmentIndex < fragments.size()){
									fragmentTransaction.hide(fragments.get(currentFragmentIndex));
								}
								fragmentTransaction.show(fragments.get(1));
							}
							fragmentTransaction.commit();
							currentFragmentIndex = 1;
						}
						break;
					case 2:
						if (currentFragmentIndex != 2){
							fragmentTransaction = fragmentManager.beginTransaction();
							if (!fragments.get(2).isAdded()){
								fragmentTransaction.add(R.id.frame_action_container,fragments.get(2));
							}else {
								fragments.get(2).onStart();
								if (currentFragmentIndex >=0 && currentFragmentIndex < fragments.size()){
									fragmentTransaction.hide(fragments.get(currentFragmentIndex));
								}
								fragmentTransaction.show(fragments.get(2));
							}
							fragmentTransaction.commit();
							currentFragmentIndex = 2;
						}
						break;
				}

				return false;
			}
		});
	}




}
